<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="姓名" prop="name">
            <el-input
               v-model="queryParams.name"
               placeholder="请输入姓名"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="电话" prop="phone">
            <el-input
               v-model="queryParams.phone"
               placeholder="请输入电话"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd"
               
            >新增</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="tableData">
         <el-table-column label="序号" align="center" type="index" width="50" />
         <el-table-column label="id" align="center" prop="id" min-width="100">
         </el-table-column>
         <el-table-column label="姓名" align="center" prop="name" :show-overflow-tooltip="true" />
         <el-table-column label="账号" align="center" prop="user_name" min-width="150"></el-table-column>
         <el-table-column label="电话" align="center" prop="phone" min-width="150" />
         <el-table-column label="负责账号" align="center" prop="" min-width="150" />
         <el-table-column label="超管" align="center" prop="" min-width="150">
            <template #default="scope">
               <el-tag type="success" v-if="scope.row.role === 2">超管</el-tag>
            </template>
         </el-table-column>
         <el-table-column label="创建时间" align="center" prop="createdAt" min-width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" width="140" fixed="right" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" >修改</el-button>
               <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" >删除</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.page"
         v-model:limit="queryParams.size"
         @pagination="getList"
      />

      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" v-model="open" width="700px" append-to-body>
         <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
            <el-row>
               <el-col :span="24">
                  <el-form-item label="账号" prop="user_name">
                     <el-input v-model="form.user_name" placeholder="请输入" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="密码" prop="password">
                     <el-input v-model="form.password" placeholder="请输入" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="姓名" prop="name">
                     <el-input v-model="form.name" placeholder="请输入" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="手机" prop="phone">
                     <el-input v-model="form.phone" placeholder="请输入" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="邮箱" prop="email">
                     <el-input v-model="form.email" placeholder="请输入" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="负责账号" prop="">
                     <el-input v-model="form.email111" placeholder="请输入" />
                  </el-form-item>
               </el-col>
               
               <el-col :span="24">
                  <el-form-item label="超管" prop="role">
                     <el-input v-model="form.role" placeholder="请输入" />
                  </el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="UserManage">
import { getPageList, addForm, editForm, deleteData } from "@/api/dudu";


const { proxy } = getCurrentInstance();
const apiUrl = '/users';
const tableData = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    size: 10,
    name: '',
    phone: '',
  },
  rules: {
    name: [{ required: true, message: "标题不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询页面列表 */
function getList() {
  loading.value = true;
  getPageList(apiUrl, queryParams.value).then(res => {
   console.log(res);
    tableData.value = res.result.list;
    total.value = res.result.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
   form.value = {
      name: "",
      user_name: "",
      phone: "",
      email: null,
      password: "",
      rank: null,
      role: null,
   };
  
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = "修改";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
         editForm(apiUrl, form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
         addForm(apiUrl,form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除？').then(function() {
    return deleteData(apiUrl, row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
